package com.springboot.cloud.demos.ribbon.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.exception.SystemErrorType;
import com.springboot.cloud.demos.entity.form.MomentCommentForm;
import com.springboot.cloud.demos.entity.po.MomentComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
public class MomentCommentService {

    @Autowired
    RestTemplate restTemplate;

    @SentinelResource(fallback = "saveFallback")
    public Result save(@RequestBody MomentCommentForm momentCommentForm) {
        return restTemplate.postForEntity("http://producer/momentComment", momentCommentForm, Result.class).getBody();
    }

    @SentinelResource(fallback = "queryMomentCommentFallback")
    public Result queryMomentComment(String momentId,int  pageNo,int pageSize) {
        return restTemplate.getForEntity("http://producer/momentComment/query?momentId={1}&pageNo={2}&pageSize={3}", Result.class, momentId, pageNo, pageSize).getBody();
    }
    @SentinelResource(fallback = "queryMomentCommentFallback")
    public Result queryCount(String momentId) {
        return restTemplate.getForEntity("http://producer/momentComment/queryCount?momentId={1}", Result.class, momentId).getBody();
    }

    public Result saveFallback(@RequestBody MomentCommentForm momentCommentForm) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }
    public Result queryMomentCommentFallback() {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

}
